<?php

require_once 'classes/LightPressPlugin.php';

class AboutPost extends LightPressPlugin {
    
    var $constructor_args = array(
        'separator'         => 'separator',
        'cat_single'        => 'category',
        'cat_multi'         => 'categories',
        'add'               => 'You can add a new one by filling in the <a href="{option_url}/{post_permalink}#add_comment">form</a> at the bottom of this page.',
        'comments_none'     => 'No comments have been posted yet.',
        'comments_one'      => 'There is <a href="{option_url}/{post_permalink}#show_comments" title="Read comment">one reader\'s comment</a> to this post.',
        'comments_many'     => 'There are <a href="{option_url}/{post_permalink}#show_comments" title="Read comments">%s readers\' comments</a> to this post.',
        'comments_closed'   => 'This post does not accept comments.');
    var $default_context = LP_CONTEXT_POST;
    var $description = 'Display relevant information for the current post';
    
    var $active = true;
    var $hooks = array('parse_post');
    
    var $separator = ', ';
    var $cat_single = 'category';
    var $cat_multi = 'categories';
    var $add = 'You can add a new one by filling in the <a href="{option_url}/{post_permalink}#add_comment">form</a> at the bottom of this page.';
    var $comments_none = 'No comments have been posted yet.';
    var $comments_one = 'There is <a href="{option_url}/{post_permalink}#show_comments" title="Read comment">one reader\'s comment</a> to this post.';
    var $comments_many = 'There are <a href="{option_url}/{post_permalink}#show_comments" title="Read comments">%s readers\' comments</a> to this post.';
    var $comments_closed = 'This post does not accept comments.';
    
    var $_messages = array();
            
    
    function AboutPost(&$frontend, $args, $dummy_run=false) {

        $this->_frontend =& $frontend;
        
        foreach ($this->constructor_args as $k=>$v) {
            
            if (isset($args[$k]))
                $this->$k = stripslashes($args[$k]);

            $this->_messages[$k] =& $this->$k;

        }
        
        if (is_null($this->separator))
            $this->separator = $frontend->options['category_separator'];
    }
    
    function run($hook, &$post) {
        $tpl =& $this->_frontend->tpl;
        $tpl->setFile('plugin_aboutpost', 'plugins/about_post.xml');
        $tpl->setBlock('plugin_aboutpost', 'plugin_aboutpost_cat', 'PLUGIN_ABOUTPOST_CAT');
        
        $categories =& $this->_frontend->getCategories();
        
        $i = 0; $limit = count($post['categories']) - 1;
        foreach ($post['categories'] as $post_category) {
            $tpl->setVar(array(
                'plugin_aboutpost_cat_id'           => $post_category,
                'plugin_aboutpost_cat_name'         => $categories[$post_category]['cat_name'],
                'plugin_aboutpost_cat_permalink'    => $categories[$post_category]['category_permalink'],
                'plugin_aboutpost_cat_nicename'     => $categories[$post_category]['category_nicename'],
                'plugin_aboutpost_cat_sep'          => ($i != $limit) ? $this->separator : ''));
            $tpl->parse('PLUGIN_ABOUTPOST_CAT', 'plugin_aboutpost_cat', true);
            $i++;
        }
        if (count($post['categories']) == 1)
            $cat_msg = $this->_messages['cat_single'];
        else
            $cat_msg = $this->_messages['cat_multi'];
            
        switch ($post['post_comments']) {
                case 0:
                    $msg = $this->_messages['comments_none'];
                    break;
                case 1:
                    $msg = $this->_messages['comments_one'];
                    break;
                default:
                    $msg = sprintf($this->_messages['comments_many'], $post['post_comments']);
        }
        // check ping_status
        if ($post['ping_status'] == 'closed')
            $tpl->setBlock('plugin_aboutpost', 'plugin_aboutpost_trackback', 'PLUGIN_ABOUTPOST_TRACKBACK');
        $tpl->setVar(array(
            'plugin_aboutpost_comments_msg' => "$msg ",
            'plugin_aboutpost_add_msg'      => ($post['comment_status'] == 'closed' ? $this->_messages['comments_closed'] : $this->_messages['add']),
            'plugin_aboutpost_cat_msg'      => $cat_msg,
            'PLUGIN_ABOUTPOST_TRACKBACK'    => ''
            ));
            
        $tpl->parse('PLUGIN_ABOUTPOST', 'plugin_aboutpost');
    }
    
}

?>